/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.crystalrange.documents.web;

import com.googlecode.genericdao.search.Search;
import java.util.List;
import java.util.Set;
import org.crystalrange.BaseController;
import org.crystalrange.documents.model.DocumentType;
import org.crystalrange.documents.service.DocumentTypeService;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zul.Listbox;
import org.zkoss.zul.Listitem;
import org.zkoss.zul.Messagebox;
import org.zkoss.zul.Window;

/**
 *
 * @author Krzysiek
 */
public class DocumentTypeListController extends BaseController {

    protected DocumentTypeService documentTypeService;
    protected DocumentType selectedDocumentType;
    protected List<DocumentType> documentTypes;
    protected Listbox listboxDocumentType;
    protected Search searchDocumentType;

    public DocumentTypeService getDocumentTypeService() {
        return documentTypeService;
    }

    public void setDocumentTypeService(DocumentTypeService documentTypeService) {
        this.documentTypeService = documentTypeService;
    }

    public DocumentType getSelectedDocumentType() {
        return selectedDocumentType;
    }

    public void setSelectedDocumentType(DocumentType selectedDocumentType) {
        this.selectedDocumentType = selectedDocumentType;
    }

    public List<DocumentType> getDocumentTypes() {
        return documentTypes;
    }

    public void setDocumentTypes(List<DocumentType> documentTypes) {
        this.documentTypes = documentTypes;
    }
    
    public void onClick$toolbarbuttonDocumentTypeCreate(Event event) throws Exception {
        this.documentTypeCreate();
    }
    
    public void onClick$menuitemDocumentTypeCreate(Event event) throws Exception {
        this.documentTypeCreate();
    }
            
    public void onClick$toolbarbuttonDocumentTypeEdit(Event event) throws Exception {
        this.documentTypeEdit();
    }
    
    public void onClick$menuitemDocumentTypeEdit(Event event) throws Exception {
        this.documentTypeEdit();
    }
    
    public void onClick$toolbarbuttonDocumentTypeDelete(Event event) throws Exception {
        this.documentTypeDelete();
    }
    
    public void onClick$menuitemDocumentTypeDelete(Event event) throws Exception {
        this.documentTypeDelete();
    }
    
    public void onClick$toolbarbuttonDocumentTypeSearch(Event event) throws Exception {
        this.documentTypeSearch();
    }
    
    public void onClick$menuitemDocumentTypeSearch(Event event) throws Exception {
        this.documentTypeSearch();
    }
    
    public void onClick$toolbarbuttonDocumentTypeRefresh(Event event) throws Exception {
        this.documentTypeRefresh();
    }
    
    public void onClick$menuitemDocumentTypeRefresh(Event event) throws Exception {
        this.documentTypeRefresh();
    }
    
    @Override
    public void doAfterCompose(Component component) throws Exception {
        super.doAfterCompose(component);
        this.searchDocumentType = new Search(DocumentType.class);
        this.searchDocumentType.setMaxResults(100);
        this.searchDocumentType.addFetch("createdBy");
        this.documentTypes = this.documentTypeService.search(this.searchDocumentType);
        component.setAttribute("controller", this);
    }
    
    
    private void documentTypeCreate() throws Exception {
        this.execution.sendRedirect("~./zul/html/documentType.zul");
    }
    
    private void documentTypeEdit() throws Exception {
        if (this.selectedDocumentType != null) {
            this.execution.sendRedirect("~./zul/html/documentType.zul?Id=" + this.selectedDocumentType.getId().toString());
        }
    }
    
    private void documentTypeDelete() throws Exception {
        Set<Listitem> selectedItems = this.listboxDocumentType.getSelectedItems();

        if (selectedItems.size() > 0) {
            if (Messagebox.YES == Messagebox.show("Delete these records?", "Delete?",
                    Messagebox.YES | Messagebox.NO, Messagebox.QUESTION)) {
                for (Listitem selectedItem : selectedItems) {
                    DocumentType documentType = (DocumentType) selectedItem.getValue();
                    this.documentTypeService.remove(documentType);
                }
                this.selectedDocumentType = null;
            }
        } else {
            this.message.setText("Please select at least one item!");
            this.message.setIcon("/img/exclamation.png");
        }

        this.documentTypes = this.documentTypeService.search(this.searchDocumentType);
        this.binder.loadAll();
    }
    
    private void documentTypeSearch() throws Exception {
        this.execution.setAttribute("searchDocumentType", this.searchDocumentType);

        Window window = (Window) this.execution.createComponents("~./zul/html/documentTypeSearch.zul", null, null);
        window.setParent(self);
        window.doModal();

        if (this.execution.getAttribute("result") == "ok") {
            this.searchDocumentType = (Search) this.execution.getAttribute("searchDocumentType");
        }

        this.documentTypes = this.documentTypeService.search(this.searchDocumentType);
        this.binder.loadAll();
    }
    
    private void documentTypeRefresh() throws Exception {
        this.documentTypes = this.documentTypeService.search(this.searchDocumentType);
        this.binder.loadAll();
    }
}
